package com.glory.hikvisionmobile.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.MaterialAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.bean.TrackOut.TrackOutLoadBean;
import com.glory.hikvisionmobile.bean.material.XPdaMLot;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.customView.HikLabelEditText;
import com.glory.hikvisionmobile.customView.HikTabView;
import com.glory.hikvisionmobile.dialog.MaterialCountInputDialog;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;

import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * *****************************
 *
 * @Anthor yazhou
 * @Email 1184545990@qq.com
 * @Time 2020/3/26 13:56
 * @DESC *****************************
 */
public class Work_MaterialSelectActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.lbMaterialName)
    HikLabelEditText lbMaterialName;
    @BindView(R.id.lbSAPMaterialId)
    HikLabelEditText lbSAPMaterialId;
    @BindView(R.id.tabView)
    HikTabView tabView;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.bottom_tv1)
    TextView bottomTv1;

    MaterialAdapter materialAdapter;

    String lotId;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_work_material_select;
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("物料选择");
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @Override
    protected void initView() {

        _initTabs();

        materialAdapter = new MaterialAdapter(null);
        materialAdapter.setOnItemClickListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(materialAdapter);

        bottomTv1.setOnClickListener(v -> {
            loadData(lotId);
        });
    }

    private void _initTabs() {
        HikTabView.Tab tab1 = new HikTabView.Tab("物料名称", 2, Gravity.CENTER);
        HikTabView.Tab tab2 = new HikTabView.Tab("物料描述", 2, Gravity.CENTER);
        HikTabView.Tab tab3 = new HikTabView.Tab("物料批号", 2, Gravity.CENTER);
        HikTabView.Tab tab4 = new HikTabView.Tab("SAP物料批号", 2, Gravity.CENTER);
        HikTabView.Tab tab5 = new HikTabView.Tab("领用数量", 2, Gravity.CENTER);
        tabView.setTabs(tab1, tab2, tab3, tab4, tab5);
    }

    @Override
    protected void initData() {
        lotId = getIntent().getStringExtra("lotId");

        loadData(lotId);
    }

    public void loadData(String lotId){
        Map map = new LinkedHashMap();
        map.put("lotId",lotId);
        String charg = lbSAPMaterialId.getLabelContext().getText().toString().trim();
        if (!TextUtils.isEmpty(charg)) {
            map.put("charg", charg);
        }
        String materialName = lbMaterialName.getLabelContext().getText().toString().trim();
        if (!TextUtils.isEmpty(materialName)) {
            map.put("materialName", materialName);
        }
        NetUtils.requestMapDara("HIK.WuLiaoTouLiaoTrackOut",this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<TrackOutLoadBean>>(
                new TypeToken<BaseResponseBean<TrackOutLoadBean>>() {
                }.getType(), getSupportFragmentManager(), this){

            @Override
            public void onSuccess(Response<BaseResponseBean<TrackOutLoadBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())) {
                    materialAdapter.setNewData(response.body().getBody().getmLots());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<TrackOutLoadBean>> response) {
                super.onError(response);
                loadError(response.getException(),"HIK.WuLiaoTouLiaoTrackOut");

            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        MaterialCountInputDialog.show(getSupportFragmentManager(), count -> {
            if (count > 0) {
                XPdaMLot mLot = (XPdaMLot) adapter.getData().get(position);

                XPdaMLot xPdaMLot = new XPdaMLot();
                xPdaMLot.setCharg(mLot.getCharg());
                xPdaMLot.setLYQty(mLot.getLYQty());
                xPdaMLot.setMaterialName(mLot.getMaterialName());
                xPdaMLot.setMaterialDesc(mLot.getMaterialDesc());
                xPdaMLot.setmLotId(mLot.getmLotId());
                xPdaMLot.setMainQty(String.valueOf(count));

                Bundle bundle = new Bundle();
                bundle.putSerializable("XPdaMLot", xPdaMLot);
                setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
                finish();
            }
        });
    }
}
